package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.SpuAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.SpuListItemVO;
import cn.tedu.csmall.product.service.ISpuService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@RestController
@RequestMapping("/spu")
@Slf4j
@Api(tags = "8.spu管理模块")
public class SpuController {

    @Autowired
    private ISpuService spuService;

    @ApiOperation("新增spu")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(SpuAddNewDTO spuAddNewDTO){
        log.debug("开始处理【新增spu】请求");
        spuService.addNew(spuAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除spu")
    @ApiOperationSupport(order = 20)
    @PostMapping("/{id:[0-9]+}/deleteById")
    @ApiImplicitParam(name = "id",value = "spuId",required = true,dataType = "long")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("开始处理【根据id删除spu】请求");
        spuService.deleteById(id);
        return JsonResult.ok();
    }



    @ApiOperation("spu列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<SpuListItemVO>> list(){
        log.debug("开始处理【获取spu列表】请求");
        List<SpuListItemVO> list = spuService.list();
        return JsonResult.ok(list);
    }
}
